package com.cynen.p42u.task;

import com.cynen.p42u.pojo.JavDict;
import com.cynen.p42u.pojo.JavInfo;
import com.cynen.p42u.pojo.JavLibInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.scheduler.BloomFilterDuplicateRemover;
import us.codecraft.webmagic.scheduler.QueueScheduler;
import us.codecraft.webmagic.selector.Selectable;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 实际的爬虫,只负责抓取数据.处理数据.
 */
public class PicScrawp implements PageProcessor {

    private Site site = null;
    // 初始化时,需要传入参数.
    public PicScrawp(Map<String,String> info){
        site = Site.me()
                .setCharset(info.get("charset"))
                .setDomain(info.get("Domain"))
                .setSleepTime(3000)
                .setRetryTimes(3)
                .setTimeOut(5000)
                .setUserAgent(info.get("userAgent"))
                .addHeader("Cookie",info.get("Cookie"));
    }
/*    private  Site site = Site.me()
            .setRetrySleepTime(1000)
            .setRetryTimes(2)
            .setTimeOut(10000)
            .setUserAgent("Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36") //必须要.
            .addHeader("Cookie","cf_clearance=b7a5ee91f60ec59166698fc08b1792effe63710d-1588481665-0-150") //cookie中只需要此记录即可
            .setCharset("utf8");*/
    @Override
    public Site getSite() {
        // 目前无法进行配置,应用启动时就加载了,此时配置文件还没有被加载.
        return site;
    }

    @Override
    public void process(Page page) {
        if (page.getUrl().toString().length() == 29){
            // 分类查询页,全部加入到schedule中.
            page.addTargetRequests(
                    page.getHtml().css("div#rightcolumn div.genreitem").links().all()
            );
        }else {
            // 有可能是详情页,也有可能是列表页.
            List<Selectable> nodes = page.getHtml().css("div.videos div.video").nodes();
            if (nodes.size() > 0){
                // 列表页,抓取所有的url,加入到scheduler中.
                // 获得单个影片信息,加入抓取队列.
                List<Selectable> list = page.getHtml().css("div.videos div.video").nodes();
                for (int i = 0; i < list.size(); i++) {
                    String url = list.get(i).links().toString();
                    page.addTargetRequest(url);
                }
                // 获得下一页,加入抓取队列.
                // Selectable links = page.getHtml().css("div.page_selector a.next").links();
                String nextPageUrl = page.getHtml().css("div.page_selector a.next").links().toString();
                page.addTargetRequest(nextPageUrl);

            }else {
                // 判断是否是详情页.
                if (page.getUrl().toString().contains("vl_genre.php")){
                    // 列表页,只是没有数据
                    return ;
                }
                // 详情页,获取影片信息.并解析图片信息.保存.

                JavLibInfo javLibInfo = new JavLibInfo();
                List<JavDict> javDictList = new ArrayList();

                // 影片详情页
                javLibInfo.setVideoUrl(page.getUrl().toString());
                // 影片标题
                javLibInfo.setVideoTitle(page.getHtml().css("div#video_title a","text").toString());
                // 影片编码
                javLibInfo.setVideoId(page.getHtml().css("div#video_id td.text","text").toString());
                // pic
                javLibInfo.setPicUrl(page.getHtml().css("div#video_jacket img","src").toString());
                javLibInfo.setVideoDate(page.getHtml().css("div#video_date td.text","text").toString());

                String length = page.getHtml().css("div#video_length span.text","text").toString();
                String unit = page.getHtml().css("div#video_length td").nodes().get(1).css("td","text").toString();
                javLibInfo.setVideoLength(length+unit);

                javLibInfo.setVideoDirector(page.getHtml().css("div#video_director td.text a","text").toString());
                javLibInfo.setVideoMaker(page.getHtml().css("div#video_maker td.text a","text").toString());
                javLibInfo.setVideoLabel(page.getHtml().css("div#video_label td.text a","text").toString());



                //类别
                List<String> categorys = page.getHtml().css("div#video_genres td.text a", "text").all();
                String genre = "";
                if (categorys.size() > 0){
                    for (int i = 0; i < categorys.size(); i++) {
                        if (i > 0){
                            genre += ",";
                        }
                        genre += categorys.get(i);

                        JavDict dict = new JavDict();
                        dict.setVideoId(javLibInfo.getVideoId());
                        dict.setDictName("category");
                        dict.setDictValue(categorys.get(i));
                        javDictList.add(dict);

                    }
                }
                javLibInfo.setVideoGenres(genre); //影片信息的主分类.


                // 演员
                List<String> casts = page.getHtml().css("div#video_cast td.text span.cast a","text").all();
                String cast = "";
                for (int i = 0; i < casts.size(); i++) {
                    if (i > 0){
                        cast = cast + ",";
                    }
                    cast +=casts.get(i);
                    //
                    JavDict dict = new JavDict();
                    dict.setVideoId(javLibInfo.getVideoId());
                    dict.setDictName("actor");
                    dict.setDictValue(casts.get(i));
                    javDictList.add(dict);
                }
                javLibInfo.setVideoCast(cast);
                javLibInfo.setDownloads(0);

                // 组合实体类.
                JavInfo javInfo  = new JavInfo();
                javInfo.setJavLibInfo(javLibInfo);
                javInfo.setDictList(javDictList);

                page.putField("javLibInfo",javLibInfo);
                page.putField("javInfo",javInfo); // 组合实体类.
            } /*end of page download*/
        }
    }
    @Autowired
    private JavInfoPipline javInfoPipline;

    // initialDelay 应用启动后,隔多久,开始任务.
    // fixedDelay 上一次任务执行完毕后,再次执行任务的间隔.
/*    @Scheduled(initialDelay = 1000,fixedDelay = 100*1000)
    public void job(){
        System.out.println("爬虫抓取开始.....");
        Spider.create(new PicScrawp())
                .addUrl(initUrl) // 初始的url
                .setScheduler(new QueueScheduler().setDuplicateRemover(new BloomFilterDuplicateRemover(1000000)))
                .thread(10)
                .addPipeline(javInfoPipline)
                .run();
    }*/
}
